package engineUtil;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import entity.Player;

public class InputHandler {

	/*
	 * This class will decouple the input from the user devices and 
	 * 
	 * keybounds 
	 * 
	 * start
	 * exit
	 * 
	 * forward
	 * back
	 * left
	 * right
	 * down/crouch
	 * jump/up
	 * 
	 * attack
	 * alt attack
	 * 
	 * inventory
	 * next invetory
	 * prev invetory
	 *
	 * mouse input is based on the mouse control code by oskarVeerhoek
	 * at https://github.com/OskarVeerhoek/YouTube-tutorials/blob/master
	 * /src/episode_19/MineFront.java
	 * but does not work correctly so it is disabled.
	 * 
	 */
	private boolean keyEscape;
	
    private Player player;
    public InputHandler()
	{
		
    	try {
			Keyboard.create();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}//
	public InputHandler(Player player)
	{
		this.setPlayer(player);
		
		
	}//
	public void initializeKeyboard()
	{
		try {
			Keyboard.create();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Keyboard.
		
	}
	public void pollForInput()
	{
		
	}
	
	public void mouseHandler(float dt)
	{
	
	
		float xTemp=Mouse.getDX()*player.getLookSpeed()*dt;
		float yTemp=Mouse.getDY()*player.getLookSpeed()*dt;
		if(Mouse.isGrabbed())
		{
			
		if(player.getRotY()+xTemp>= 360)
		{
			player.setRotY(player.getRotY()+xTemp-360);
			
		} else if(player.getRotY()+xTemp<0)
		{
			player.setRotY(360-player.getRotY()+xTemp);
		}
		else
		{
			player.setRotY(xTemp+player.getRotY());
		}
		
		if(player.getRotX()-yTemp>=player.getMaxLook()[1]&&player.getRotX()-yTemp<=player.getMaxLook()[0])
		{
		
			
			player.setRotX(player.getRotX()-yTemp);
		
		
		}
		
		else if(player.getRotX()-yTemp<player.getMaxLook()[1])
		{
			player.setRotX(player.getMaxLook()[1]);
		}
		else if(player.getRotX()-yTemp<player.getMaxLook()[0])
		{
			player.setRotX(player.getMaxLook()[0]);
		}
		
		}
	}
	
	public void keyboardHandler(float dt)
	{
		boolean keyUp;
		
		 boolean keyDown ;
		
	     boolean keyLeft ;
	     boolean keyRight;
	     boolean keyLookUp;
	     boolean keyLookDown;
	     boolean keyJump ;
	     boolean keyCrouch ;
	     
	     
		keyUp = Keyboard.isKeyDown(Keyboard.KEY_W);
		
		keyDown = Keyboard.isKeyDown(Keyboard.KEY_S);
		 
		keyLeft = Keyboard.isKeyDown(Keyboard.KEY_A);
		 
		 keyRight = Keyboard.isKeyDown(Keyboard.KEY_D);
		 keyLookUp=Keyboard.isKeyDown(Keyboard.KEY_R);
		 keyLookDown=Keyboard.isKeyDown(Keyboard.KEY_F);
		 
		 boolean keyTurnRight=Keyboard.isKeyDown(Keyboard.KEY_E);
		 boolean keyTurnleft=Keyboard.isKeyDown(Keyboard.KEY_Q);
		 
		 keyJump = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
		 keyCrouch = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
		// keyEscape = Keyboard.();
		 
		float distance=player.getMoveSpeed()*dt;
		
		if(keyUp)
		{
			
			
			player.moveForward(distance);
		
		}
		
		if(keyDown)
		{
			player.moveBack(distance);
		}
		
		if(keyLeft)
		{
			player.moveLeft(distance);
		}
		
		if(keyRight)
		{
			player.moveRight(distance);
		}
		
		if(keyJump)
		{
			player.moveUp(distance);
			//player.setPosY(player.getPosY()+1);
		}
		
		if(keyCrouch)
		{
			player.moveDown(distance);
			//player.setPosY(player.getPosY()-1);
		}
		
		if(keyTurnRight)
		{
			player.turnRight();
		}
		
		if(keyTurnleft)
		{
			player.turnLeft();
		}
		
		if(keyLookUp)
		{
			player.turnUp();
		}
		if(keyLookDown)
		{
			player.turnDown();
		}
       
		if(keyEscape)
		{
			
			
			
			
			if(Mouse.isGrabbed())
			{
			Mouse.setGrabbed(false);//if the mouse is grabbed release it
			}	
			else
			{
			Mouse.setGrabbed(true);//if the mouse is not grabbed grab it
			}
			
		}
		
	}

	/**
	 * @return the player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * @param player the player to set
	 */
	public void setPlayer(Player player) {
		this.player = player;
	}
	
	
}
